#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
    int min(int a, int b)
    {
        if (a > b) return b;
        else return a;
    }
    vector<vector<int>> matrixBlockSum(const vector<vector<int>>& mat, int k) {
        vector<vector<int>> dp, ret;
        dp.resize(mat.size() + 1);
        dp[0].resize(mat[0].size() + 1);
        for (int i = 1; i <= mat.size(); i++)
        {
            dp[i].resize(mat[i - 1].size() + 1);
            for (int j = 1; j <= mat[i - 1].size(); j++)
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + mat[i - 1][j - 1];
        }
        ret.resize(mat.size());
        for (int i = 0; i < mat.size(); i++)
        {
            ret[i].resize(mat[i].size());
            for (int j = 0; j < mat[i].size(); j++)
            {
                int x1, y1, x2, y2;
                x1 = max(0, i - k), y1 = max(0, j - k);
                x2 = min(mat.size() - 1, i + k), y2 = min(mat[i].size() - 1, j + k);
                ret[i][j] = dp[x2 + 1][y2 + 1] - dp[x2 + 1][y1] - dp[x1][y2 + 1] + dp[x1][y1];
            }
        }
        return ret;
    }
};

int main()
{
    Solution s;
    vector<vector<int>> ret = s.matrixBlockSum({ {1, 2, 3},{4, 5, 6},{7, 8, 9 } }, 1);
    return 0;
}